package com.lez.flowpractice2.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.lez.flowpractice2.databinding.ItemBinding
import com.lez.flowpractice2.db.User

class UserAdapter(private val context: Context):RecyclerView.Adapter<BindingViewHolder>() {

    private val data = ArrayList<User>()

    fun setData(data:List<User>){
        this.data.clear()
        this.data.addAll(data)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BindingViewHolder {
        val binding = ItemBinding.inflate(LayoutInflater.from(context),parent,false)
        return BindingViewHolder(binding)
    }

    override fun onBindViewHolder(holder: BindingViewHolder, position: Int) {
        val user = data[position]
        val binding = holder.binding as ItemBinding
        binding.apply {
            itemId.text = "${user.id}"
            itemFirstName.text = "${user.firstName}"
            itemLastName.text = "${user.lastName}"
        }
    }

    override fun getItemCount(): Int {
        return data.size
    }

}